package com.lechi.web.controller;

import com.lechi.web.common.annotation.Log;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.common.enums.BusinessType;
import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.common.utils.SecurityUtils;
import com.lechi.web.service.IIotDeviceGroupService;
import com.lechi.web.service.IIotDeviceService;
import com.lechi.web.system.domain.IotDeviceBo;
import com.lechi.web.system.domain.IotDeviceGroupBo;
import com.lechi.web.vo.IotDeviceGroupVo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备组Controller
 *
 * @author yangguang
 */
@RestController
@Validated
@RequestMapping("/iot/group")
@RequiredArgsConstructor
public class IotDeviceGroupController extends BaseController {

    private final IIotDeviceGroupService iotDeviceGroupService;

    private final IIotDeviceService iotDeviceService;

    /**
     * 查询设备组列表
     *
     * @param iotDeviceGroupVo the iot device group vo
     * @return the table data info
     */
    @GetMapping("/list")
    //    @PreAuthorize("@ss.hasPermi('iot:device:query')")
    public TableDataInfo list(IotDeviceGroupVo iotDeviceGroupVo) {
        startPage();
        IotDeviceGroupBo iotDeviceGrou = new IotDeviceGroupBo();
        BeanUtils.copyProperties(iotDeviceGroupVo, iotDeviceGrou);
        List<IotDeviceGroupBo> groupList = iotDeviceGroupService.selectIotDeviceGroupList(iotDeviceGrou);
        return getDataTable(groupList);
    }

    /**
     * 获取设备组详细信息
     *
     * @param id the id
     * @return the info
     */
    @GetMapping(value = "/{id}")
    //    @PreAuthorize("@ss.hasPermi('iot:device:query')")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iotDeviceGroupService.selectIotDeviceGroupById(id));
    }

    /**
     * 新增设备组
     *
     * @param iotDeviceGroupVo the iot device group vo
     * @return the ajax result
     */
    @PostMapping("/add")
    @Log(title = "新增设备组操作", businessType = BusinessType.INSERT)
    //    @PreAuthorize("@ss.hasPermi('iot:device:add')")
    public AjaxResult add(@RequestBody @Valid IotDeviceGroupVo iotDeviceGroupVo) {
        IotDeviceGroupBo iotDeviceGrou = new IotDeviceGroupBo();
        BeanUtils.copyProperties(iotDeviceGroupVo, iotDeviceGrou);
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        iotDeviceGrou.setCreateBy(username);
        iotDeviceGroupService.insertIotDeviceGroup(iotDeviceGrou);
        return AjaxResult.success();
    }

    /**
     * 修改设备组
     *
     * @param iotDeviceGroupVo the iot device group vo
     * @return the ajax result
     */
    @PostMapping("/edit")
    @Log(title = "编辑设备操作", businessType = BusinessType.UPDATE)
    //    @PreAuthorize("@ss.hasPermi('iot:device:edit')")
    public AjaxResult edit(@RequestBody @Valid IotDeviceGroupVo iotDeviceGroupVo) {
        IotDeviceGroupBo iotDeviceGroupBo = new IotDeviceGroupBo();
        BeanUtils.copyProperties(iotDeviceGroupVo, iotDeviceGroupBo);
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        iotDeviceGroupBo.setUpdateBy(username);
        iotDeviceGroupService.updateIotDeviceGroup(iotDeviceGroupBo);
        return AjaxResult.success();
    }

    /**
     * 删除设备组
     *
     * @param id the id
     * @return the ajax result
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除设备组操作", businessType = BusinessType.UPDATE)
    //    @PreAuthorize("@ss.hasPermi('iot:device:remove')")
    public AjaxResult remove(@PathVariable Long id) {
        IotDeviceBo iotDeviceBo = new IotDeviceBo();
        iotDeviceBo.setGroupId(id);
        List<IotDeviceBo> iotDeviceBos = iotDeviceService.selectIotDeviceList(iotDeviceBo);
        if (iotDeviceBos != null && !iotDeviceBos.isEmpty()) {
            throw new ServiceException(ErrorCodeEnum.DELETE_GROUP_ERROR.getValue(), ErrorCodeEnum.DELETE_GROUP_ERROR.getKey());
        }
        iotDeviceGroupService.deleteIotDeviceGroupById(id);
        return AjaxResult.success();
    }
}
